package com.hhwy.framework.persistent.entity;

import java.io.Serializable;
import java.util.Iterator;
import java.util.Map;

import com.hhwy.framework.persistent.entity.DProperty;
import com.hhwy.framework.persistent.entity.DRelation;

/**
 * <b>类 名 称：</b>DomainInfo 实体描述信息，类型、属性类型、关系类型<br/>
 * <b>类 描 述：</b><br/>
 * <b>创 建 人：</b>lihuiming<br/>
 * <b>修 改 人：</b>Administrator<br/>
 * <b>修改时间：</b>2014年8月4日 上午8:51:10<br/>
 * <b>修改备注：</b><br/>
 * @version 1.0.0<br/>
 */
public class DomainInfo implements Serializable {

	private static final long serialVersionUID = 8099302904554663716L;

	public final Class<?> entityClass;
	public final int propertySize;
	public final int relationSize;
	public final String entityName;

	public final Map<String, DProperty> propertyMap;
	public final Map<String, DRelation> relationMap;

	public DRelation getRelationByClass(String clazz){
		DRelation relation = null;
		
		Iterator<String> it = relationMap.keySet().iterator();
		
		while(it.hasNext()){
			String relationName = it.next();
			DRelation r = relationMap.get(relationName);
			if(r.getTargetClass() != null && r.getTargetClass().equals(clazz)){
				relation = relationMap.get(relationName);
				break;
			}
			
		}
		return relation;
	}
	public DRelation getRelationByMappedBy(String mappedBy){
		DRelation relation = null;
		
		Iterator<String> it = relationMap.keySet().iterator();
		
		while(it.hasNext()){
			String relationName = it.next();
			DRelation r = relationMap.get(relationName);
			if(r.getName() != null && r.getName().equals(mappedBy)){
				relation = relationMap.get(relationName);
				break;
			}
			
		}
		return relation;
	}
	public DomainInfo(Class<?> clazz, String entityName,
			Map<String, DProperty> propertyMap,
			Map<String, DRelation> relationMap) {
		this.entityClass = clazz;
		this.entityName = entityName;
		this.propertySize = propertyMap.size();
		this.relationSize = relationMap.size();

		this.propertyMap = propertyMap;
		this.relationMap = relationMap;

	}
}
